package org.music.generate.model;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by mingdongc on 2017/3/8.
 */
public class ColumnMetaData{

    private String name; // 字段名称
    private String javaName;
    private String javaGetSetName;
    private int type; // 字段类型
    private String jdbcTypeName; // 字段类型名称
    private Object value; // 值
    private String javaTypeName;

    final static Map<String,String> javaTypeMap;

    static {
        javaTypeMap=new HashMap<String,String>();
        javaTypeMap.put("TIMESTAMP","Date");
        javaTypeMap.put("INTEGER","Integer");
        javaTypeMap.put("INT","Integer");
        javaTypeMap.put("VARCHAR","String");
        javaTypeMap.put("CHAR","String");
        javaTypeMap.put("DECIMAL","BigDecimal");
        javaTypeMap.put("DECIMAL UNSIGNED","BigDecimal");
        javaTypeMap.put("DATE","Date");
    }

    public ColumnMetaData(String columnName, int valueType, String jdbcTypeName) {
        this.name = columnName;
        this.type = valueType;
        this.jdbcTypeName = jdbcTypeName;
        if("DATETIME".equals(jdbcTypeName)){
            this.jdbcTypeName="TIMESTAMP";
        }
        if("INT".equals(jdbcTypeName)){
            this.jdbcTypeName="INTEGER";
        }
        if(javaTypeMap.containsKey(this.jdbcTypeName)){
            this.javaTypeName=javaTypeMap.get(this.jdbcTypeName);
        }else{
            throw new RuntimeException("没有找到"+this.javaTypeName+"对应的javaType");
        }

        String name = StringUtils.uncapitalize(columnName);
        String[] ns=StringUtils.split(name,"_");
        StringBuffer sb=new StringBuffer(ns[0]);
        for(int i=1;i<ns.length;i++){
            sb.append(StringUtils.capitalize(ns[i]));
        }
        this.javaName=sb.toString();
        this.javaGetSetName=StringUtils.capitalize(this.javaName);

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getJavaName() {
        return javaName;
    }

    public void setJavaName(String javaName) {
        this.javaName = javaName;
    }

    public String getJavaGetSetName() {
        return javaGetSetName;
    }

    public void setJavaGetSetName(String javaGetSetName) {
        this.javaGetSetName = javaGetSetName;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getJdbcTypeName() {
        return jdbcTypeName;
    }

    public void setJdbcTypeName(String jdbcTypeName) {
        this.jdbcTypeName = jdbcTypeName;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public String getJavaTypeName() {
        return javaTypeName;
    }

    public void setJavaTypeName(String javaTypeName) {
        this.javaTypeName = javaTypeName;
    }
}